package com.jeomo.mem.provider.web;

import cn.hutool.core.date.DateUtil;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.common.core.request.PageRequest;
import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.util.BeanCopyUtil;
import com.jeomo.common.core.vo.PageResponseVo;
import com.jeomo.mem.provider.request.MemberActivityRequest;
import com.jeomo.mem.provider.request.MemberPointsActivityRuleListRequest;
import com.jeomo.mem.provider.request.MemberPointsActivityRuleRequest;
import com.jeomo.mem.provider.vo.MemberPointsActivityVo;
import com.jeomo.mem.starter.dto.MemberPointActivityDto;
import com.jeomo.mem.starter.entity.MemberPointsActivityExe;
import com.jeomo.mem.starter.entity.MemberPointsActivityRule;
import com.jeomo.mem.starter.entity.MemberPointsActivityRuleList;
import com.jeomo.mem.starter.service.IMemPointsActivityRuleService;
import com.jeomo.mem.starter.service.IMemPointsActivityService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员积分活动规则
 * @author:Jackpot
 * @create 2020-09-30 12:24
 * @description:
 */
@RestController
@RequestMapping("/member/activity")
public class MemPointsActivityRuleController {

    @Autowired
    private IMemPointsActivityRuleService memActivityRuleService;

    @Autowired
    private IMemPointsActivityService memActivityService;

    @ApiOperation("会员积分活动规则定义接口")
    @PostMapping("/rule")
    public CommonResult saveRule(@RequestBody @ApiParam("会员积分规则") MemberActivityRequest memberActivityRequest){

        MemberPointsActivityRuleRequest memActivityRuleRequest = memberActivityRequest.getMemPointsActivityRule();
        List<MemberPointsActivityRuleListRequest> memberActivityRuleListRequest = memberActivityRequest.getMemActivityRuleList();

        MemberPointsActivityRule memberPointsRule = BeanCopyUtil.copyProperties(memActivityRuleRequest, MemberPointsActivityRule.class);
        List<MemberPointsActivityRuleList> memberPointsRuleList =
                BeanCopyUtil.copyListProperties(memberActivityRuleListRequest, MemberPointsActivityRuleList::new);

        int resultCode = memActivityRuleService.saveRule(memberPointsRule, memberPointsRuleList);

        if (resultCode >0){
            return CommonResult.success("保存成功");
        }else if (resultCode == -1){
            return CommonResult.failure("数据已发生变化,请刷新后重试！");
        }else if (resultCode == -2){
            return CommonResult.failure("数据已被删除，操作失败！");
        }else{
            return CommonResult.failure("保存失败");
        }
    }

    @ApiOperation("获取一条积分活动规则的详情信息")
    @GetMapping("/rule/{ruleCode}")
    public CommonResult getPointRuleById(@PathVariable String ruleCode){

        MemberPointActivityDto memberPointRule = memActivityRuleService.getMemberPointRule(ruleCode);
        return CommonResult.success(memberPointRule);
    }

    @ApiOperation("分页获取会员积分活动规则")
    @PostMapping("/rules/page")
    public CommonResult<PageResponseVo<MemberPointsActivityVo>> getPointRulesByPage(@RequestBody PageRequest pageRequest){
        PageQueryDto dto = BeanCopyUtil.copyProperties(pageRequest, PageQueryDto.class);
        PageResponseDto pageResponseDto = memActivityService.pageSearch(dto);
        PageResponseVo<MemberPointsActivityVo> memPointsRulePageResponseVo = BeanCopyUtil.copyPageProperties(pageResponseDto, MemberPointsActivityVo.class);
        return CommonResult.success(memPointsRulePageResponseVo);
    }

    @ApiOperation("计算出符合要求的积分活动")
    @GetMapping("/figure")
    public CommonResult getActivities(@RequestParam String levelCode,@RequestParam String goodsCode,@RequestParam double amount){
        Date curDate = DateUtil.beginOfDay(new Date());
        Map<String, MemberPointsActivityExe> activities = memActivityRuleService.getActivities(levelCode, goodsCode, amount,curDate);
        return CommonResult.success(activities);
    }

    @ApiOperation("删除单条会员积分活动规则")
    @DeleteMapping("/rule/{ruleCode}")
    public CommonResult deletePointRule(@PathVariable String ruleCode){
        int i = memActivityRuleService.deleteRule(ruleCode);
        return i>0 ? CommonResult.success("删除成功"):CommonResult.failure("操作失败");
    }

    @ApiOperation("停用/启用会员积分活动规则")
    @PostMapping("/rule/status/{ruleCode}/{status}")
    public CommonResult updateRuleStatus(@PathVariable String ruleCode,@PathVariable boolean status){

        boolean i = memActivityService.updateRuleStatus(ruleCode, status);
        return i?CommonResult.success("操作成功"):CommonResult.failure("操作失败");
    }

}
